from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI 


class addTransformInv(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        # Unique R variables
        self.setRvariableNames(["invAdditive"])
        
        # Python variables
        self.data = {}
        self.RFunctionParam_x = ''
        
        # Inputs
        self.inputs.addInput("x", "x", signals.base.RMatrix, self.processx)
        
        # Outputs
        self.outputs.addOutput("Inv. addTransform Output","Inv. addTransform Output", signals.base.RMatrix)
        
        # GUI
        area = redRGUI.base.widgetBox(self.controlArea,orientation='vertical')
        compType = redRGUI.base.groupBox(area, label='Select composition type to go back to:')
        self.size = redRGUI.base.radioButtons(compType,  label = "Size signifiance", 
        buttons = ['Yes','No'], setChecked='No',orientation='horizontal', callback = self.onTestChange)
        self.scale = redRradioButtons(compType,  label = "Natural scale", 
        buttons = [('real','Difference (real)'),('log','Quotient (log)')], setChecked='log',orientation='horizontal', callback = self.onTestChange)
        self.total = redRGUI.base.lineEdit(compType, label = "total:", text = '1')
        self.method = redRGUI.base.lineEdit(compType, label = "Method:", text = 'acomp')
        self.method.setDisabled(True)
        self.invAddOpLabel = redRGUI.base.lineEdit(area, label = "Inverse additive operation:", text = 'Inv. Additive log transform')
        self.invAddOpLabel.setDisabled(True)  
        self.invAddOpCommand = 'alrInv'
        
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)

    def onTestChange(self):
        if self.size.getChecked() =='Yes' and self.scale.getChecked() =='Difference (real)' :
            self.method.setText('rplus')
            self.invAddOpLabel.setText('Inv. Isometric identity transform')
            self.invAddOpCommand = 'iitInv'
            self.total.setDisabled(True)
        elif self.size.getChecked() =='Yes' and self.scale.getChecked() =='Quotient (log)' :
            self.method.setText('aplus')
            self.invAddOpLabel.setText('Inv. Isometric log transform')
            self.invAddOpCommand = 'iltInv'
            self.total.setDisabled(True)
        elif self.size.getChecked() =='No' and self.scale.getChecked() =='Difference (real)' :
            self.method.setText('rcomp')
            self.invAddOpLabel.setText('Inv. Additive planar transform')
            self.invAddOpCommand = 'aptInv'
            self.total.setEnabled(True)
        elif self.size.getChecked() =='No' and self.scale.getChecked() =='Quotient (log)' :
            self.method.setText('acomp')
            self.invAddOpLabel.setText('Inv. Additive log transform')
            self.invAddOpCommand = 'alrInv'
            self.total.setEnabled(True)

    def processx(self, data):
        if not self.require_librarys(["compositions"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_x=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_x=''
    def commitFunction(self):
        if str(self.RFunctionParam_x) == '':
            self.status.setText('Data is missing.')
            return

        # Compute transformation
        self.R(self.Rvariables['invAdditive']+'<-'+self.invAddOpCommand+'(z='+str(self.RFunctionParam_x)+')', processingNotice=True)
        newData = signals.base.RMatrix(self, data = self.Rvariables['invAdditive'], checkVal = False)
        
        # Send to output signal
        self.rSend("Inv. addTransform Output", newData)